package 比特教务管理系统.双链表;

public class 第2题 {
    public static void main(String[] args) {

    }
    class Node1 {
        public int data;
        public Node1 next;

        public Node1(int data) {
            this.data = data;
        }

        public Node1() {

        }
    }

    class LinkList {
        Node1 head = new Node1();
        public Node1 detectCircle() {
            Node1 fast = this.head;//定义fast指针
            Node1 slow = this.head;//定义slow指针
            while (fast != null && fast.next != null) {
                fast = fast.next.next;//fast走两步
                slow = slow.next;//slow走一步
                //二者相遇
                if (fast == slow) {
                    break;
                }
            }
            //链表无环的情况
            if (fast == null || fast.next == null) {
                return null;
            }
            fast = this.head;//让fast回到head
            while (fast != slow) {
                fast= fast.next;
                slow = slow.next;
            }
            return fast;
        }

    }
}
